package com.gxa.scdx.cloud.user.util;

import com.gxa.scdx.cloud.user.service.impl.DataServiceImpl;
import org.json.JSONException;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

//@author 颜可翔

public class ServerThread extends Thread {

    /**
     * socket连接对象
     */
    private Socket socket;
    /**
     * 输入、输出流
     */

    private InputStreamReader inputStreamReader;
    private DataOutputStream dataOutputStream;

    /**
     * 数据处理的对象
     */
    private DataServiceImpl dataService;

    /**
     * 构造函数
     * @param socket
     */
    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    /**
     * 包含处理数据对象的构造函数
     * @param socket
     * @param dataService
     */
    public ServerThread(Socket socket, DataServiceImpl dataService) {
        this.socket = socket;
        this.dataService = dataService;
    }

    @Override
    public void run() {

        try {
            // 获取客户端信息
            InetAddress inetAddress = socket.getInetAddress();
            // ip
            String ipAddress = inetAddress.getHostAddress();
            System.out.println("客户端: " + ipAddress + "连接成功");
            // 准备输入流
            inputStreamReader = new InputStreamReader(socket.getInputStream(), "UTF-8");
            // 准备输出流
            dataOutputStream = new DataOutputStream(socket.getOutputStream());

            String recvData = getData(inputStreamReader);
//            System.out.println("客户端：" + recvData);

            // 处理数据
            dataService.fileProcess(recvData);

        } catch (IOException  e) {
            e.printStackTrace();
        } finally {
            // 清理资源
            closeResources(inputStreamReader, dataOutputStream, socket);
        }

        super.run();
    }

    /**
     * 获取输入数据的方法
     * @param inputStreamReader
     * @return
     */
    private String getData(InputStreamReader inputStreamReader) throws IOException {
        // 准备一个char数组用于接收缓存传递的数据
        char[] chars = new char[1024];
        // 读取字节的大小
        int read;
        // 新建字符串缓冲用于保存传递的字节流
        StringBuffer stringBuffer = new StringBuffer();
        while ((read = inputStreamReader.read(chars)) != -1){
            // 持续读取Python端传递的数据
//            System.out.println("读取了" + read + "个字节");
            stringBuffer.append(new String(chars, 0, read));
        }
//        System.out.println(stringBuffer.toString());
        // 将存储在stringBuffer中的数据转换为字符串传入后续的处理
        return stringBuffer.toString();
    }

    /**
     * 回收资源的方法
     * @param dataInputStream
     * @param dataOutputStream
     * @param socket
     */
    private void closeResources(InputStreamReader dataInputStream, DataOutputStream dataOutputStream, Socket socket){
        try {
            if (dataInputStream != null) {
                dataInputStream.close();
            }
            if (dataOutputStream != null) {
                dataOutputStream.close();
            }
            if (socket != null){
                socket.close();
                System.out.println("关闭连接--------------------------------------------");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
